home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gsalphac.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  25.1 KB  |  829 lines

  1. /* Copyright (C) 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gsalphac.c,v 1.2 2000/09/19 19:00:25 lpd Exp $ */
  20. /* Alpha-compositing implementation */
  21. #include "memory_.h"
  22. #include "gx.h"
  23. #include "gserrors.h"
  24. #include "gsalphac.h"
  25. #include "gsiparam.h"        /* for gs_image_alpha_t */
  26. #include "gsutil.h"        /* for gs_next_ids */
  27. #include "gxalpha.h"
  28. #include "gxcomp.h"
  29. #include "gxdevice.h"
  30. #include "gxgetbit.h"
  31. #include "gxlum.h"
  32.  
  33. /* ---------------- Internal definitions ---------------- */
  34.  
  35. /* Define the parameters for a compositing operation. */
  36. typedef struct gs_composite_params_s {
  37.     gs_composite_op_t cop;
  38.     float delta;        /* only for dissolve */
  39.     uint source_alpha;        /* only if !psource->alpha */
  40.     uint source_values[4];    /* only if !psource->data */
  41. } gs_composite_params_t;
  42.  
  43. /* Define the source or destination for a compositing operation. */
  44. #define pixel_row_fields(elt_type)\
  45.   elt_type *data;\
  46.   int bits_per_value;    /* 1, 2, 4, 8, 12, 16 */\
  47.   int initial_x;\
  48.   gs_image_alpha_t alpha
  49. typedef struct pixel_row_s {
  50.     pixel_row_fields(byte);
  51. } pixel_row_t;
  52. typedef struct const_pixel_row_s {
  53.     pixel_row_fields(const byte);
  54. } const_pixel_row_t;
  55.  
  56. /*
  57.  * Composite two arrays of (premultiplied) pixel values.  Legal values of
  58.  * values_per_pixel are 1-4, not including alpha.  Note that if pdest->alpha
  59.  * is "none", the alpha value for all destination pixels will be taken as
  60.  * unity, and any operation that could generate alpha values other than
  61.  * unity will return an error.  "Could generate" means that there are
  62.  * possible values of the source and destination alpha values for which the
  63.  * result has non-unity alpha: the error check does not scan the actual
  64.  * alpha data to test whether there are any actual values that would
  65.  * generate a non-unity alpha result.
  66.  */
  67. int composite_values(P5(const pixel_row_t * pdest,
  68.             const const_pixel_row_t * psource,
  69.             int values_per_pixel, uint num_pixels,
  70.             const gs_composite_params_t * pcp));
  71.  
  72. /* ---------------- Alpha-compositing objects ---------------- */
  73.  
  74. /*
  75.  * Define which operations can generate non-unity alpha values in 3 of the 4
  76.  * cases of source and destination not having unity alphas.  (This is always
  77.  * possible in the fourth case, both S & D non-unity, except for CLEAR.)  We
  78.  * do this with a bit mask indexed by the operation, counting from the LSB.
  79.  * The name indicates whether S and/or D has non-unity alphas.
  80.  */
  81. #define alpha_out_notS_notD\
  82.   (1<<composite_Dissolve)
  83. #define _alpha_out_either\
  84.   (alpha_out_notS_notD|(1<<composite_Satop)|(1<<composite_Datop)|\
  85.     (1<<composite_Xor)|(1<<composite_PlusD)|(1<<composite_PlusL))
  86. #define alpha_out_S_notD\
  87.   (_alpha_out_either|(1<<composite_Copy)|(1<<composite_Sover)|\
  88.     (1<<composite_Din)|(1<<composite_Dout))
  89. #define alpha_out_notS_D\
  90.   (_alpha_out_either|(1<<composite_Sin)|(1<<composite_Sout)|\
  91.     (1<<composite_Dover)|(1<<composite_Highlight))
  92.  
  93. /* ------ Object definition and creation ------ */
  94.  
  95. /* Define alpha-compositing objects. */
  96. private composite_create_default_compositor_proc(c_alpha_create_default_compositor);
  97. private composite_equal_proc(c_alpha_equal);
  98. private composite_write_proc(c_alpha_write);
  99. private composite_read_proc(c_alpha_read);
  100. private const gs_composite_type_t gs_composite_alpha_type =
  101. {
  102.     {
  103.     c_alpha_create_default_compositor,
  104.     c_alpha_equal,
  105.     c_alpha_write,
  106.     c_alpha_read
  107.     }
  108. };
  109. typedef struct gs_composite_alpha_s {
  110.     gs_composite_common;
  111.     gs_composite_alpha_params_t params;
  112. } gs_composite_alpha_t;
  113.  
  114. gs_private_st_simple(st_composite_alpha, gs_composite_alpha_t,
  115.              "gs_composite_alpha_t");
  116.  
  117. /* Create an alpha-compositing object. */
  118. int
  119. gs_create_composite_alpha(gs_composite_t ** ppcte,
  120.           const gs_composite_alpha_params_t * params, gs_memory_t * mem)
  121. {
  122.     gs_composite_alpha_t *pcte;
  123.  
  124.     rc_alloc_struct_0(pcte, gs_composite_alpha_t, &st_composite_alpha,
  125.               mem, return_error(gs_error_VMerror),
  126.               "gs_create_composite_alpha");
  127.     pcte->type = &gs_composite_alpha_type;
  128.     pcte->id = gs_next_ids(1);
  129.     pcte->params = *params;
  130.     *ppcte = (gs_composite_t *) pcte;
  131.     return 0;
  132. }
  133.  
  134. /* ------ Object implementation ------ */
  135.  
  136. #define pacte ((const gs_composite_alpha_t *)pcte)
  137.  
  138. private bool
  139. c_alpha_equal(const gs_composite_t * pcte, const gs_composite_t * pcte2)
  140. {
  141.     return (pcte2->type == pcte->type &&
  142. #define pacte2 ((const gs_composite_alpha_t *)pcte2)
  143.         pacte2->params.op == pacte->params.op &&
  144.         (pacte->params.op != composite_Dissolve ||
  145.          pacte2->params.delta == pacte->params.delta));
  146. #undef pacte2
  147. }
  148.  
  149. private int
  150. c_alpha_write(const gs_composite_t * pcte, byte * data, uint * psize)
  151. {
  152.     uint size = *psize;
  153.     uint used;
  154.  
  155.     if (pacte->params.op == composite_Dissolve) {
  156.     used = 1 + sizeof(pacte->params.delta);
  157.     if (size < used) {
  158.         *psize = used;
  159.         return_error(gs_error_rangecheck);
  160.     }
  161.     memcpy(data + 1, &pacte->params.delta, sizeof(pacte->params.delta));
  162.     } else {
  163.     used = 1;
  164.     if (size < used) {
  165.         *psize = used;
  166.         return_error(gs_error_rangecheck);
  167.     }
  168.     }
  169.     *data = (byte) pacte->params.op;
  170.     *psize = used;
  171.     return 0;
  172. }
  173.  
  174. private int
  175. c_alpha_read(gs_composite_t ** ppcte, const byte * data, uint size,
  176.          gs_memory_t * mem)
  177. {
  178.     gs_composite_alpha_params_t params;
  179.  
  180.     if (size < 1 || *data > composite_op_last)
  181.     return_error(gs_error_rangecheck);
  182.     params.op = *data;
  183.     if (params.op == composite_Dissolve) {
  184.     if (size != 1 + sizeof(params.delta))
  185.         return_error(gs_error_rangecheck);
  186.     memcpy(¶ms.delta, data + 1, sizeof(params.delta));
  187.     } else {
  188.     if (size != 1)
  189.         return_error(gs_error_rangecheck);
  190.     }
  191.     return gs_create_composite_alpha(ppcte, ¶ms, mem);
  192. }
  193.  
  194. /* ---------------- Alpha-compositing device ---------------- */
  195.  
  196. /* Define the default alpha-compositing device. */
  197. typedef struct gx_device_composite_alpha_s {
  198.     gx_device_forward_common;
  199.     gs_composite_alpha_params_t params;
  200. } gx_device_composite_alpha;
  201.  
  202. gs_private_st_suffix_add0_final(st_device_composite_alpha,
  203.              gx_device_composite_alpha, "gx_device_composite_alpha",
  204.     device_c_alpha_enum_ptrs, device_c_alpha_reloc_ptrs, gx_device_finalize,
  205.                 st_device_forward);
  206. /* The device descriptor. */
  207. private dev_proc_close_device(dca_close);
  208. private dev_proc_fill_rectangle(dca_fill_rectangle);
  209. private dev_proc_map_rgb_color(dca_map_rgb_color);
  210. private dev_proc_map_color_rgb(dca_map_color_rgb);
  211. private dev_proc_copy_mono(dca_copy_mono);
  212. private dev_proc_copy_color(dca_copy_color);
  213. private dev_proc_map_rgb_alpha_color(dca_map_rgb_alpha_color);
  214. private dev_proc_map_color_rgb_alpha(dca_map_color_rgb_alpha);
  215. private dev_proc_copy_alpha(dca_copy_alpha);
  216. private const gx_device_composite_alpha gs_composite_alpha_device =
  217. {std_device_std_body_open(gx_device_composite_alpha, 0,
  218.               "alpha compositor", 0, 0, 1, 1),
  219.  {gx_default_open_device,
  220.   gx_forward_get_initial_matrix,
  221.   gx_default_sync_output,
  222.   gx_default_output_page,
  223.   dca_close,
  224.   dca_map_rgb_color,
  225.   dca_map_color_rgb,
  226.   dca_fill_rectangle,
  227.   gx_default_tile_rectangle,
  228.   dca_copy_mono,
  229.   dca_copy_color,
  230.   gx_default_draw_line,
  231.   gx_default_get_bits,
  232.   gx_forward_get_params,
  233.   gx_forward_put_params,
  234.   gx_default_cmyk_map_cmyk_color,    /* only called for CMYK */
  235.   gx_forward_get_xfont_procs,
  236.   gx_forward_get_xfont_device,
  237.   dca_map_rgb_alpha_color,
  238.   gx_forward_get_page_device,
  239.   gx_forward_get_alpha_bits,
  240.   dca_copy_alpha,
  241.   gx_forward_get_band,
  242.   gx_default_copy_rop,
  243.   gx_default_fill_path,
  244.   gx_default_stroke_path,
  245.   gx_default_fill_mask,
  246.   gx_default_fill_trapezoid,
  247.   gx_default_fill_parallelogram,
  248.   gx_default_fill_triangle,
  249.   gx_default_draw_thin_line,
  250.   gx_default_begin_image,
  251.   gx_default_image_data,
  252.   gx_default_end_image,
  253.   gx_default_strip_tile_rectangle,
  254.   gx_default_strip_copy_rop,
  255.   gx_forward_get_clipping_box,
  256.   gx_default_begin_typed_image,
  257.   gx_forward_get_bits_rectangle,
  258.   dca_map_color_rgb_alpha,
  259.   gx_no_create_compositor
  260.  }
  261. };
  262.  
  263. /* Create an alpha compositor. */
  264. private int
  265. c_alpha_create_default_compositor(const gs_composite_t * pcte,
  266.        gx_device ** pcdev, gx_device * dev, const gs_imager_state * pis,
  267.                   gs_memory_t * mem)
  268. {
  269.     gx_device_composite_alpha *cdev;
  270.  
  271.     if (pacte->params.op == composite_Copy) {
  272.     /* Just use the original device. */
  273.     *pcdev = dev;
  274.     return 0;
  275.     }
  276.     cdev =
  277.     gs_alloc_struct_immovable(mem, gx_device_composite_alpha,
  278.                   &st_device_composite_alpha,
  279.                   "create default alpha compositor");
  280.     *pcdev = (gx_device *)cdev;
  281.     if (cdev == 0)
  282.     return_error(gs_error_VMerror);
  283.     gx_device_init((gx_device *)cdev,
  284.            (const gx_device *)&gs_composite_alpha_device, mem, true);
  285.     gx_device_copy_params((gx_device *)cdev, dev);
  286.     /*
  287.      * Set the color_info and depth to be compatible with the target,
  288.      * but using standard chunky color storage, including alpha.
  289.      ****** CURRENTLY ALWAYS USE 8-BIT COLOR ******
  290.      */
  291.     cdev->color_info.depth =
  292.     (dev->color_info.num_components == 4 ? 32 /* CMYK, no alpha */ :
  293.      (dev->color_info.num_components + 1) * 8);
  294.     cdev->color_info.max_gray = cdev->color_info.max_color = 255;
  295.     /* No halftoning will occur, but we fill these in anyway.... */
  296.     cdev->color_info.dither_grays = cdev->color_info.dither_colors = 256;
  297.     /*
  298.      * We could speed things up a little by tailoring the procedures in
  299.      * the device to the specific num_components, but for simplicity,
  300.      * we'll defer considering that until there is a demonstrated need.
  301.      */
  302.     gx_device_set_target((gx_device_forward *)cdev, dev);
  303.     cdev->params = pacte->params;
  304.     return 0;
  305. }
  306.  
  307. /* Close the device and free its storage. */
  308. private int
  309. dca_close(gx_device * dev)
  310. {                /*
  311.                  * Finalization will call close again: avoid a recursion loop.
  312.                  */
  313.     set_dev_proc(dev, close_device, gx_default_close_device);
  314.     gs_free_object(dev->memory, dev, "dca_close");
  315.     return 0;
  316. }
  317.  
  318. /* ------ (RGB) color mapping ------ */
  319.  
  320. private gx_color_index
  321. dca_map_rgb_color(gx_device * dev,
  322.           gx_color_value r, gx_color_value g, gx_color_value b)
  323. {
  324.     return dca_map_rgb_alpha_color(dev, r, g, b, gx_max_color_value);
  325. }
  326. private gx_color_index
  327. dca_map_rgb_alpha_color(gx_device * dev,
  328.           gx_color_value red, gx_color_value green, gx_color_value blue,
  329.             gx_color_value alpha)
  330. {                /*
  331.                  * We work exclusively with premultiplied color values, so we
  332.                  * have to premultiply the color components by alpha here.
  333.                  */
  334.     byte a = gx_color_value_to_byte(alpha);
  335.  
  336. #define premult_(c)\
  337.   (((c) * a + gx_max_color_value / 2) / gx_max_color_value)
  338. #ifdef PREMULTIPLY_TOWARDS_WHITE
  339.     byte bias = ~a;
  340.  
  341. #  define premult(c) (premult_(c) + bias)
  342. #else
  343. #  define premult(c) premult_(c)
  344. #endif
  345.     gx_color_index color;
  346.  
  347.     if (dev->color_info.num_components == 1) {
  348.     uint lum =
  349.     (red * lum_red_weight + green * lum_green_weight +
  350.      blue * lum_blue_weight + lum_all_weights / 2) /
  351.     lum_all_weights;
  352.  
  353.     if (a == 0xff)
  354.         color = gx_color_value_to_byte(lum);
  355.     else            /* Premultiplication is necessary. */
  356.         color = premult(lum);
  357.     } else {
  358.     if (a == 0xff)
  359.         color =
  360.         ((uint) gx_color_value_to_byte(red) << 16) +
  361.         ((uint) gx_color_value_to_byte(green) << 8) +
  362.         gx_color_value_to_byte(blue);
  363.     else            /* Premultiplication is necessary. */
  364.         color =
  365.         (premult(red) << 16) + (premult(green) << 8) + premult(blue);
  366.     }
  367. #undef premult
  368.     return (color << 8) + a;
  369. }
  370. private int
  371. dca_map_color_rgb(gx_device * dev, gx_color_index color,
  372.           gx_color_value prgb[3])
  373. {
  374.     gx_color_value red = gx_color_value_from_byte((byte) (color >> 24));
  375.     byte a = (byte) color;
  376.  
  377. #define postdiv_(c)\
  378.   (((c) * 0xff + a / 2) / a)
  379. #ifdef PREMULTIPLY_TOWARDS_WHITE
  380.     byte bias = ~a;
  381.  
  382. #  define postdiv(c) postdiv_(c - bias)
  383. #else
  384. #  define postdiv(c) postdiv_(c)
  385. #endif
  386.  
  387.     if (dev->color_info.num_components == 1) {
  388.     if (a != 0xff) {
  389.         /* Undo premultiplication. */
  390.         if (a == 0)
  391.         red = 0;
  392.         else
  393.         red = postdiv(red);
  394.     }
  395.     prgb[0] = prgb[1] = prgb[2] = red;
  396.     } else {
  397.     gx_color_value
  398.         green = gx_color_value_from_byte((byte) (color >> 16)),
  399.         blue = gx_color_value_from_byte((byte) (color >> 8));
  400.  
  401.     if (a != 0xff) {
  402.         /* Undo premultiplication. */
  403. /****** WHAT TO DO ABOUT BIG LOSS OF PRECISION? ******/
  404.         if (a == 0)
  405.         red = green = blue = 0;
  406.         else {
  407.         red = postdiv(red);
  408.         green = postdiv(green);
  409.         blue = postdiv(blue);
  410.         }
  411.     }
  412.     prgb[0] = red, prgb[1] = green, prgb[2] = blue;
  413.     }
  414. #undef postdiv
  415.     return 0;
  416. }
  417. private int
  418. dca_map_color_rgb_alpha(gx_device * dev, gx_color_index color,
  419.             gx_color_value prgba[4])
  420. {
  421.     prgba[3] = gx_color_value_from_byte((byte) color);
  422.     return dca_map_color_rgb(dev, color, prgba);
  423. }
  424.  
  425. /* ------ Imaging ------ */
  426.  
  427. private int
  428. dca_fill_rectangle(gx_device * dev, int x, int y, int w, int h,
  429.            gx_color_index color)
  430. {                /* This is where all the real work gets done! */
  431.     gx_device_composite_alpha *adev = (gx_device_composite_alpha *) dev;
  432.     gx_device *target = adev->target;
  433.     byte *std_row;
  434.     byte *native_row;
  435.     gs_int_rect rect;
  436.     gs_get_bits_params_t std_params, native_params;
  437.     int code = 0;
  438.     int yi;
  439.     gs_composite_params_t cp;
  440.     const_pixel_row_t source;
  441.     pixel_row_t dest;
  442.  
  443.     fit_fill(dev, x, y, w, h);
  444.     std_row = gs_alloc_bytes(dev->memory,
  445.                  (dev->color_info.depth * w + 7) >> 3,
  446.                  "dca_fill_rectangle(std)");
  447.     native_row = gs_alloc_bytes(dev->memory,
  448.                 (target->color_info.depth * w + 7) >> 3,
  449.                 "dca_fill_rectangle(native)");
  450.     if (std_row == 0 || native_row == 0) {
  451.     code = gs_note_error(gs_error_VMerror);
  452.     goto out;
  453.     }
  454.     rect.p.x = x, rect.q.x = x + w;
  455.     std_params.options =
  456.     gb_colors_for_device(dev) |
  457.     (GB_ALPHA_LAST | GB_DEPTH_8 | GB_PACKING_CHUNKY |
  458.      GB_RETURN_COPY | GB_RETURN_POINTER | GB_ALIGN_ANY |
  459.      GB_OFFSET_0 | GB_OFFSET_ANY | GB_RASTER_STANDARD |
  460.      GB_RASTER_ANY);
  461.     cp.cop = adev->params.op;
  462.     if (cp.cop == composite_Dissolve)
  463.     cp.delta = adev->params.delta;
  464.     {
  465.     gx_color_value rgba[4];
  466.  
  467. /****** DOESN'T HANDLE CMYK ******/
  468.     (*dev_proc(dev, map_color_rgb_alpha)) (dev, color, rgba);
  469.     cp.source_values[0] = gx_color_value_to_byte(rgba[0]);
  470.     cp.source_values[1] = gx_color_value_to_byte(rgba[1]);
  471.     cp.source_values[2] = gx_color_value_to_byte(rgba[2]);
  472.     cp.source_alpha = gx_color_value_to_byte(rgba[3]);
  473.     }
  474.     source.data = 0;
  475.     source.bits_per_value = 8;
  476.     source.alpha = gs_image_alpha_none;
  477.     for (yi = y; yi < y + h; ++yi) {
  478.     /* Read a row in standard representation. */
  479.     rect.p.y = yi, rect.q.y = yi + 1;
  480.     std_params.data[0] = std_row;
  481.     code = (*dev_proc(target, get_bits_rectangle))
  482.         (target, &rect, &std_params, NULL);
  483.     if (code < 0)
  484.         break;
  485.     /* Do the work. */
  486.     dest.data = std_params.data[0];
  487.     dest.bits_per_value = 8;
  488.     dest.initial_x =
  489.         (std_params.options & GB_OFFSET_ANY ? std_params.x_offset : 0);
  490.     dest.alpha =
  491.         (std_params.options & GB_ALPHA_FIRST ? gs_image_alpha_first :
  492.          std_params.options & GB_ALPHA_LAST ? gs_image_alpha_last :
  493.          gs_image_alpha_none);
  494.     code = composite_values(&dest, &source,
  495.                 dev->color_info.num_components, w, &cp);
  496.     if (code < 0)
  497.         break;
  498.     if (std_params.data[0] == std_row) {
  499.         /* Convert the row back to native representation. */
  500.         /* (Otherwise, we had a direct pointer to device data.) */
  501.         native_params.options =
  502.         (GB_COLORS_NATIVE | GB_PACKING_CHUNKY | GB_RETURN_COPY |
  503.          GB_OFFSET_0 | GB_RASTER_ALL | GB_ALIGN_STANDARD);
  504.         native_params.data[0] = native_row;
  505.         code = gx_get_bits_copy(target, 0, w, 1, &native_params,
  506.                     &std_params, std_row,
  507.                     0 /* raster is irrelevant */ );
  508.         if (code < 0)
  509.         break;
  510.         code = (*dev_proc(target, copy_color))
  511.         (target, native_row, 0, 0 /* raster is irrelevant */ ,
  512.          gx_no_bitmap_id, x, yi, w, 1);
  513.         if (code < 0)
  514.         break;
  515.     }
  516.     }
  517.   out:gs_free_object(dev->memory, native_row, "dca_fill_rectangle(native)");
  518.     gs_free_object(dev->memory, std_row, "dca_fill_rectangle(std)");
  519.     return code;
  520. }
  521.  
  522. private int
  523. dca_copy_mono(gx_device * dev, const byte * data,
  524.         int dx, int raster, gx_bitmap_id id, int x, int y, int w, int h,
  525.           gx_color_index zero, gx_color_index one)
  526. {
  527. /****** TEMPORARY ******/
  528.     return gx_default_copy_mono(dev, data, dx, raster, id, x, y, w, h,
  529.                 zero, one);
  530. }
  531.  
  532. private int
  533. dca_copy_color(gx_device * dev, const byte * data,
  534.            int dx, int raster, gx_bitmap_id id,
  535.            int x, int y, int w, int h)
  536. {
  537. /****** TEMPORARY ******/
  538.     return gx_default_copy_color(dev, data, dx, raster, id, x, y, w, h);
  539. }
  540.  
  541. private int
  542. dca_copy_alpha(gx_device * dev, const byte * data, int data_x,
  543.        int raster, gx_bitmap_id id, int x, int y, int width, int height,
  544.            gx_color_index color, int depth)
  545. {
  546. /****** TEMPORARY ******/
  547.     return gx_default_copy_alpha(dev, data, data_x, raster, id, x, y,
  548.                  width, height, color, depth);
  549. }
  550.  
  551. /*
  552.  * Composite two arrays of (premultiplied) pixel values.
  553.  * See gsdpnext.h for the specification.
  554.  *
  555.  * The current implementation is simple but inefficient.  We'll speed it up
  556.  * later if necessary.
  557.  */
  558. int
  559. composite_values(const pixel_row_t * pdest, const const_pixel_row_t * psource,
  560.    int values_per_pixel, uint num_pixels, const gs_composite_params_t * pcp)
  561. {
  562.     int dest_bpv = pdest->bits_per_value;
  563.     int source_bpv = psource->bits_per_value;
  564.  
  565.     /*
  566.      * source_alpha_j gives the source component index for the alpha value,
  567.      * if the source has alpha.
  568.      */
  569.     int source_alpha_j =
  570.     (psource->alpha == gs_image_alpha_last ? values_per_pixel :
  571.      psource->alpha == gs_image_alpha_first ? 0 : -1);
  572.  
  573.     /* dest_alpha_j does the same for the destination. */
  574.     int dest_alpha_j =
  575.     (pdest->alpha == gs_image_alpha_last ? values_per_pixel :
  576.      pdest->alpha == gs_image_alpha_first ? 0 : -1);
  577.  
  578.     /* dest_vpp is the number of stored destination values. */
  579.     int dest_vpp = values_per_pixel + (dest_alpha_j >= 0);
  580.  
  581.     /* source_vpp is the number of stored source values. */
  582.     int source_vpp = values_per_pixel + (source_alpha_j >= 0);
  583.  
  584.     bool constant_colors = psource->data == 0;
  585.     uint highlight_value = (1 << dest_bpv) - 1;
  586.  
  587.     sample_load_declare(sptr, sbit);
  588.     sample_store_declare(dptr, dbit, dbyte);
  589.  
  590.     {
  591.     uint xbit = pdest->initial_x * dest_bpv * dest_vpp;
  592.  
  593.     sample_store_setup(dbit, xbit & 7, dest_bpv);
  594.     dptr = pdest->data + (xbit >> 3);
  595.     }
  596.     {
  597.     uint xbit = psource->initial_x * source_bpv * source_vpp;
  598.  
  599.     sbit = xbit & 7;
  600.     sptr = psource->data + (xbit >> 3);
  601.     }
  602.     {
  603.     uint source_max = (1 << source_bpv) - 1;
  604.     uint dest_max = (1 << dest_bpv) - 1;
  605.  
  606.     /*
  607.      * We could save a little work by only setting up source_delta
  608.      * and dest_delta if the operation is Dissolve.
  609.      */
  610.     float source_delta = pcp->delta * dest_max / source_max;
  611.     float dest_delta = 1.0 - pcp->delta;
  612.     uint source_alpha = pcp->source_alpha;
  613.     uint dest_alpha = dest_max;
  614.  
  615. #ifdef PREMULTIPLY_TOWARDS_WHITE
  616.     uint source_bias = source_max - source_alpha;
  617.     uint dest_bias = 0;
  618.     uint result_bias = 0;
  619.  
  620. #endif
  621.     uint x;
  622.  
  623.     if (!pdest->alpha) {
  624.         uint mask =
  625.         (psource->alpha || source_alpha != source_max ?
  626.          alpha_out_S_notD : alpha_out_notS_notD);
  627.  
  628.         if ((mask >> pcp->cop) & 1) {
  629.         /*
  630.          * The operation could produce non-unity alpha values, but
  631.          * the destination can't store them.  Return an error.
  632.          */
  633.         return_error(gs_error_rangecheck);
  634.         }
  635.     }
  636.     /* Preload the output byte buffer if necessary. */
  637.     sample_store_preload(dbyte, dptr, dbit, dest_bpv);
  638.  
  639.     for (x = 0; x < num_pixels; ++x) {
  640.         int j;
  641.         uint result_alpha = dest_alpha;
  642.  
  643. /* get_value does not increment the source pointer. */
  644. #define get_value(v, ptr, bit, bpv, vmax)\
  645.   sample_load16(v, ptr, bit, bpv)
  646.  
  647. /* put_value increments the destination pointer. */
  648. #define put_value(v, ptr, bit, bpv, bbyte)\
  649.   sample_store_next16(v, ptr, bit, bpv, bbyte)
  650.  
  651. #define advance(ptr, bit, bpv)\
  652.   sample_next(ptr, bit, bpv)
  653.  
  654.         /* Get destination alpha value. */
  655.         if (dest_alpha_j >= 0) {
  656.         int dabit = dbit + dest_bpv * dest_alpha_j;
  657.         const byte *daptr = dptr + (dabit >> 3);
  658.  
  659.         get_value(dest_alpha, daptr, dabit & 7, dest_bpv, dest_max);
  660. #ifdef PREMULTIPLY_TOWARDS_WHITE
  661.         dest_bias = dest_max - dest_alpha;
  662. #endif
  663.         }
  664.         /* Get source alpha value. */
  665.         if (source_alpha_j >= 0) {
  666.         int sabit = sbit;
  667.         const byte *saptr = sptr;
  668.  
  669.         if (source_alpha_j == 0)
  670.             advance(sptr, sbit, source_bpv);
  671.         else
  672.             advance(saptr, sabit, source_bpv * source_alpha_j);
  673.         get_value(source_alpha, saptr, sabit, source_bpv, source_max);
  674. #ifdef PREMULTIPLY_TOWARDS_WHITE
  675.         source_bias = source_max - source_alpha;
  676. #endif
  677.         }
  678. /*
  679.  * We are always multiplying a dest value by a source value to compute a
  680.  * dest value, so the denominator is always source_max.  (Dissolve is the
  681.  * one exception.)
  682.  */
  683. #define fr(v, a) ((v) * (a) / source_max)
  684. #define nfr(v, a, maxv) ((v) * (maxv - (a)) / source_max)
  685.  
  686.         /*
  687.          * Iterate over the components of a single pixel.
  688.          * j = 0 for alpha, 1 .. values_per_pixel for color
  689.          * components, regardless of the actual storage order;
  690.          * we arrange things so that sptr/sbit and dptr/dbit
  691.          * always point to the right place.
  692.          */
  693.         for (j = 0; j <= values_per_pixel; ++j) {
  694.         uint dest_v, source_v, result;
  695.  
  696. #define set_clamped(r, v)\
  697.   BEGIN if ( (r = (v)) > dest_max ) r = dest_max; END
  698.  
  699.         if (j == 0) {
  700.             source_v = source_alpha;
  701.             dest_v = dest_alpha;
  702.         } else {
  703.             if (constant_colors)
  704.             source_v = pcp->source_values[j - 1];
  705.             else {
  706.             get_value(source_v, sptr, sbit, source_bpv, source_max);
  707.             advance(sptr, sbit, source_bpv);
  708.             }
  709.             get_value(dest_v, dptr, dbit, dest_bpv, dest_max);
  710. #ifdef PREMULTIPLY_TOWARDS_WHITE
  711.             source_v -= source_bias;
  712.             dest_v -= dest_bias;
  713. #endif
  714.         }
  715.  
  716.         switch (pcp->cop) {
  717.             case composite_Clear:
  718.             /*
  719.              * The NeXT documentation doesn't say this, but the CLEAR
  720.              * operation sets not only alpha but also all the color
  721.              * values to 0.
  722.              */
  723.             result = 0;
  724.             break;
  725.             case composite_Copy:
  726.             result = source_v;
  727.             break;
  728.             case composite_PlusD:
  729.             /*
  730.              * This is the only case where we have to worry about
  731.              * clamping a possibly negative result.
  732.              */
  733.             result = source_v + dest_v;
  734.             result = (result < dest_max ? 0 : result - dest_max);
  735.             break;
  736.             case composite_PlusL:
  737.             set_clamped(result, source_v + dest_v);
  738.             break;
  739.             case composite_Sover:
  740.             set_clamped(result, source_v + nfr(dest_v, source_alpha, source_max));
  741.             break;
  742.             case composite_Dover:
  743.             set_clamped(result, nfr(source_v, dest_alpha, dest_max) + dest_v);
  744.             break;
  745.             case composite_Sin:
  746.             result = fr(source_v, dest_alpha);
  747.             break;
  748.             case composite_Din:
  749.             result = fr(dest_v, source_alpha);
  750.             break;
  751.             case composite_Sout:
  752.             result = nfr(source_v, dest_alpha, dest_max);
  753.             break;
  754.             case composite_Dout:
  755.             result = nfr(dest_v, source_alpha, source_max);
  756.             break;
  757.             case composite_Satop:
  758.             set_clamped(result, fr(source_v, dest_alpha) +
  759.                     nfr(dest_v, source_alpha, source_max));
  760.             break;
  761.             case composite_Datop:
  762.             set_clamped(result, nfr(source_v, dest_alpha, dest_max) +
  763.                     fr(dest_v, source_alpha));
  764.             break;
  765.             case composite_Xor:
  766.             set_clamped(result, nfr(source_v, dest_alpha, dest_max) +
  767.                     nfr(dest_v, source_alpha, source_max));
  768.             break;
  769.             case composite_Highlight:
  770.             /*
  771.              * Bizarre but true: this operation converts white and
  772.              * light gray into each other, and leaves all other values
  773.              * unchanged.  We only implement it properly for gray-scale
  774.              * devices.
  775.              */
  776.             if (j != 0 && !((source_v ^ highlight_value) & ~1))
  777.                 result = source_v ^ 1;
  778.             else
  779.                 result = source_v;
  780.             break;
  781.             case composite_Dissolve:
  782.             /*
  783.              * In this case, and only this case, we need to worry about
  784.              * source and dest having different bpv values.  For the
  785.              * moment, we wimp out and do everything in floating point.
  786.              */
  787.             result = (uint) (source_v * source_delta + dest_v * dest_delta);
  788.             break;
  789.             default:
  790.             return_error(gs_error_rangecheck);
  791.         }
  792.         /*
  793.          * Store the result.  We don't have to worry about
  794.          * destinations that don't store alpha, because we don't
  795.          * even compute an alpha value in that case.
  796.          */
  797. #ifdef PREMULTIPLY_TOWARDS_WHITE
  798.         if (j == 0) {
  799.             result_alpha = result;
  800.             result_bias = dest_max - result_alpha;
  801.             if (dest_alpha_j != 0)
  802.             continue;
  803.         } else {
  804.             result += result_bias;
  805.         }
  806. #else
  807.         if (j == 0 && dest_alpha_j != 0) {
  808.             result_alpha = result;
  809.             continue;
  810.         }
  811. #endif
  812.         put_value(result, dptr, dbit, dest_bpv, dbyte);
  813.         }
  814.         /* Skip a trailing source alpha value. */
  815.         if (source_alpha_j > 0)
  816.         advance(sptr, sbit, source_bpv);
  817.         /* Store a trailing destination alpha value. */
  818.         if (dest_alpha_j > 0)
  819.         put_value(result_alpha, dptr, dbit, dest_bpv, dbyte);
  820. #undef get_value
  821. #undef put_value
  822. #undef advance
  823.     }
  824.     /* Store any partial output byte. */
  825.     sample_store_flush(dptr, dbit, dest_bpv, dbyte);
  826.     }
  827.     return 0;
  828. }
  829.